#!/usr/bin/env bash
# desc start - wget https://hello.fe80.cn/bash/setup.sh && bash setup.sh
#!/bin/bash
# 这是一个帮我安装程序的脚本
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
cur_dir=$(pwd)

Color_Text()
{
    echo -e " \e[0;$2m$1\e[0m"
}

Echo_Red()
{
    echo $(Color_Text "$1" "31")
}

Echo_Green()
{
    echo $(Color_Text "$1" "32")
}

Echo_Yellow()
{
    echo $(Color_Text "$1" "33")
}

Echo_Blue()
{
    echo $(Color_Text "$1" "34")
}

# Check if user is root
if [ $(id -u) != "0" ]; then
    Echo_Red "Error: 请使用root权限"
    exit 1
fi

cd ~

if [ ! -f "/root/.ssh/id_rsa" ];then
    Echo_Yellow "[NOTICE] 请先配置秘钥"
    Echo_Yellow "vim /root/.ssh/id_rsa"
    Echo_Yellow "chmod 600 /root/.ssh/id_rsa"
    exit 1
fi

# 判断目录是否存在
if [ ! -d "/data" ];then
    mkdir /data
fi

# 判断目录是否存在
if [ ! -d "/data/database" ];then
    mkdir /data/database
fi

# 判断目录是否存在
if [ ! -d "/data/code" ];then
    mkdir /data/code
fi

# 判断目录是否存在
if [ ! -d "/data/log" ];then
    mkdir /data/log
fi

# 判断目录是否存在
if [ ! -d "/data/program" ];then
    mkdir /data/program
fi

# 判断目录是否存在
if [ ! -d "/data/code/linzening" ];then
    cd /data/code/
    git clone git@gitee.com:linzening/linzening.git
fi

# 判断目录是否存在
if [ ! -d "/data/code/mugin" ];then
    cd /data/code/
    git clone git@gitee.com:linzening/mugin.git
    cd /data/code/mugin
    go build -o main
    yum install supervisor -y
    if [ ! -f "/etc/supervisor.d/ginweb.conf" ];then
        cat > /etc/supervisor.d/ginweb.conf<<EOF
[program:ginweb]
directory=/data/code/mugin
command=/data/code/mugin/main
user=root
autostart=true
autorestart=true
loglevel=info
stdout_logfile=/var/log/ginweb-stdout.log
stderr_logfile=/var/log/ginweb-stderr.log
EOF
        service supervisor restart
    fi
fi

# 判断目录是否存在
if [ ! -d "/data/code/book" ];then
    cd /data/code/
    git clone git@gitee.com:linzening/book.git
fi

# 判断目录是否存在
if [ ! -d "/data/code/beibei" ];then
    cd /data/code/
    git clone git@codeup.aliyun.com:5ff034fc578739320804fe4b/linzening/beibei.git
fi

# 判断目录是否存在
if [ ! -d "/data/code/staticfile" ];then
    cd /data/code/
    git clone git@gitee.com:comeround/staticfile.git
fi

# 判断目录是否存在
if [ ! -d "/data/code/comeround" ];then
    cd /data/code/
    git clone git@gitee.com:linzening/comeround.git
fi

#go env -w GO111MODULE=on
#go env -w GOPROXY=https://goproxy.cn,direct